//
//  LFDownloader.swift
//  XiaoXiangMu1604
//
//  Created by qianfeng on 16/9/26.
//  Copyright © 2016年 SunPiaoLiang. All rights reserved.
//

import UIKit

//下载的类型
public enum DownloadType:Int{
    
    case Default //默认值
    
    case Detail //详情数据
    
    case NearBy //详情页的数据
    
}

//协议    下载的代理
protocol LFDownloaderDelegate:NSObjectProtocol{
    
    //下载失败的方法
    func downloader(downloader:LFDownloader,didFailWithError error:NSError)
    
    //下载成功的方法
    func downloader(downloader:LFDownloader,didFinishWithData data:NSData)

}

class LFDownloader: NSObject {

    //下载类型
    var type : DownloadType = .Default
    
    //代理属性
    weak var delegate:LFDownloaderDelegate?
    
    //文件获取缓存文件的路径
    func cachePath(urlString:String)->String{
        
        ///注：MD5为一个文件加密的方法
        
        let fileName = NSString(string: urlString).MD5Hash()
        
        //沙盒路径的目录
        let docDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
        
        // 返回文件的路径
        return docDir! + "/" + fileName
        
    }
    
    //判断是否存储了缓存
    func isUrlStringDataCached(urlString:String,timeout time:NSTimeInterval)->Bool{
        
        let path = cachePath(urlString)
        
        let fm = NSFileManager.defaultManager()
        
        //如果文件存在，就说明缓存了
        if fm.fileExistsAtPath(path){
            
            let dict = try!fm.attributesOfItemAtPath(path)
            
            let createDate = dict[NSFileCreationDate] as! NSDate
            
            //计算时间差
            let timeInterval = NSDate().timeIntervalSinceDate(createDate)
            
            //(用传进来的超时时间与（此时的时间-文件创建的时间）的时间进行对比)
            if (timeInterval < time){
                
                return true
            }
            
            
        }
        
        return false
        
    }
    
    //下载的方法
    func downloadWithURLString(urlString:String){
        
        //超时时间为24小时
        if isUrlStringDataCached(urlString,timeout: 24*60*60){
            
            ////如果有缓存，则去读取缓存中的数据
            
            let path = cachePath(urlString)
            
            //读文件
            let data = NSData(contentsOfFile: path)
            
            delegate?.downloader(self, didFinishWithData: data!)
            
        }else{
            
            ////如果没有款存，则网络请求下载数据
            
            //1.创建NSURL对象
            let url = NSURL(string: urlString)
            
            //2.创建NSURLRequest对象
            let request = NSURLRequest(URL: url!)
            
            //3.获取NSURLSession对象
            let session = NSURLSession.sharedSession()
            
            //4.创建NSURLSessionDataTask对象
            let task = session.dataTaskWithRequest(request) { (data, response, error)  in
                
                if let tmpError = error{
                    
                    //下载失败
                    self.delegate?.downloader(self, didFailWithError: tmpError)
                    
                }else{
                    
                    let httpResponse = response as! NSHTTPURLResponse
                    
                    if httpResponse.statusCode == 200{
                        
                        //下载成功
                        self.delegate?.downloader(self, didFinishWithData: data!)
                        
                        //存缓存
                        let path = self.cachePath(urlString)
                        
                        // 此方法会抛异常，工作中应该解决这个异常，用do--cache
                        try!data?.writeToFile(path, options: .DataWritingAtomic)
                        
                        
                    }else{
                        
                        //下载失败
                        let err = NSError(domain: urlString, code: httpResponse.statusCode, userInfo: ["msg":"下载失败"])
                        
                        self.delegate?.downloader(self, didFailWithError: err)
                        
                    }
                    
                }
                
            }
            
            //5.开启下载
            task.resume()

        }
        
    }
    
}
